package com.core.servlets;

import java.io.IOException;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.core.hibernate.ExceptionHandler;
import com.core.hibernate.exceptionevent.ExceptionEvent;
import com.core.managers.ServletManager;
import com.core.managers.SessionManager;
import com.core.protocols.IExceptionListener;

/**
 * Servlet implementation class JiraServlet
 */
public class TaskTrackerServlet
        extends HttpServlet
        implements IExceptionListener {



	private static final long      serialVersionUID = 1L;
	protected Set <ExceptionEvent> events           = new HashSet <ExceptionEvent>();



	protected void setup () {

		events.add(new ExceptionEvent(ExceptionEvent.ANYEVENT));

	}



	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public TaskTrackerServlet() {

		super();

		this.setup();
		ExceptionHandler.sharedInstance().addListener(this, events);
	}



	public void finalize ()
	        throws Throwable {

		try {
			ExceptionHandler.sharedInstance().removeListener(this, this.events);
		} finally {
			super.finalize();
		}
	}



	@Override
	public void parseException ( Exception e ) {


	}



	@Override
	public void parseException ( Exception e,
	                             Serializable userInfo ) {


	}



	protected void doGet ( HttpServletRequest request,
	                       HttpServletResponse response )
	        throws ServletException, IOException {

		// this.getRequest = request;
		// this.getResponse = response;


		response.setHeader("Cache-Control", "no-cache");
		response.setHeader("Pragma", "no-cache");
		response.setDateHeader("Expires", 0);

		SessionManager sessionManager = SessionManager.sharedInstance();
		sessionManager.setRequest(request);
		sessionManager.setResponse(response);
		sessionManager.setUserContext(this.userContext());


		/*
		 * 
		 * Default GET redirection
		 */


		String redirection = null;

		ServletConfig servletConfig = this.getServletConfig();
		ServletManager servletManager = ServletManager.sharedInstance();
		ServletContext context = servletConfig.getServletContext();

		/*
		 * check, if any default param exists in request
		 */
		if ( servletManager.hasMethodGETRedirectionParamInRequest(request) ) {

			/*
			 * check if exists any default page for servlet
			 */
			redirection = servletConfig.getInitParameter("getRedirect");
			if ( redirection != null ) {

				/*
				 * simple forward default JSP
				 */
				RequestDispatcher dispatcher = null;
				dispatcher = context.getRequestDispatcher(redirection);
				dispatcher.forward(request, response);

			} else {

				super.doGet(request, response);

			}

		} else {


			/*
			 * append all default params for request
			 */
			redirection = servletManager.methodGETRedirectionWithDefaultParamForRequest(request, servletConfig);

			if ( redirection != null ) {

				RequestDispatcher dispatcher = null;
				dispatcher = context.getRequestDispatcher(redirection);
				dispatcher.forward(request, response);
			} else {
				super.doGet(request, response);
			}
		}

	}



	protected String userContext () {

		return null;
	}



}
